﻿using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.Production;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;
using Yitter.IdGenerator;

namespace ProduceBasicData.Api.Application.Handler.Production
{
    /// <summary>
    /// 产线管理信息删除处理类
    /// </summary>
    public class DeleteProductionHnadler : IRequestHandler<DeletetProductionCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<ProductionModel> produRepo;
        private readonly IMapper mapper;

        public DeleteProductionHnadler(IBaseRespository<ProductionModel> produRepo,
            IMapper mapper)
        {
            this.produRepo = produRepo;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(DeletetProductionCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res=new ApiResult<int>();
            var produ=await produRepo.GetModelAsync(request.Id);
            if (produ == null)
            {
                res.Code = ApiEnum.Failed;
                res.Msg = "您要删除的数据不存在";
                return res;
            }
            produ.Isdel = true;

            var r=await produRepo.Update(produ);
            if (r>0)
            {
                res.Data=r;
                res.Code = ApiEnum.Success;
                res.Msg = "产线管理信息删除成功";
            }
            else
            {
                res.Code = ApiEnum.Failed;
                res.Msg = "产线管理信息删除失败";
            }
            return res;
        }
    }
}
